package com.inspur.incubator.common;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gson.GsonBuilder;


/**
 * BigDecimal工具
 * @author Administrator
 *
 */
public class Tools {
	/**
	 * 乘法运算(参数为double)
	 * @param d1
	 * @param d2
	 * @return double
	 */
	public static double doubleMultiply(double d1, double d2) {
		BigDecimal b1 = new BigDecimal(d1);
		BigDecimal b2 = new BigDecimal(d2);
		return b1.multiply(b2).doubleValue();
	}

	/**
	 * 乘法运算(参数为BigDecimal)
	 * @param b1
	 * @param b2
	 * @return BigDecimal
	 */
	public static BigDecimal multiply(BigDecimal b1, BigDecimal b2) {
		return b1.multiply(b2);
	}

	/**
	 * 进行乘法运算(参数为String)
	 * @param String d1
	 * @param String d2
	 * @return String
	 */
	public static String mullstring(String d1, String d2) {
		BigDecimal b3 = new BigDecimal(d1);
		BigDecimal b4 = new BigDecimal(d2);
		return b3.multiply(b4).toString();
	}
	/**
	 * 进行乘法运算(参数为String)
	 * @param String d1
	 * @param String d2
	 * @return BigDecimal
	 */
	public static BigDecimal mullstrstr(String d1, String d2) {
		BigDecimal b3 = new BigDecimal(d1);
		BigDecimal b4 = new BigDecimal(d2);

		return b3.multiply(b4);
	}

	/**
	 * 比较大小
	 * @param d1
	 * @param d2
	 * @return boolean >= 返回true < 返回false
	 */
   
	public static boolean compare(BigDecimal d1, BigDecimal d2) {
		//BigDecimal b3 = d1.multiply(new BigDecimal(d2));
         if(d1.compareTo(d2) == 0){
        	 return true;
         }else if(d1.compareTo(d2) == 1){
        	 return true;
         }else if(d1.compareTo(d2) == -1){
        	 return false;
         }else{
        	 return false;
         }
		
	}
	/**
	 * 比较BigDecimal >= 0
	 * @param bd
	 * @return >= 0 返回true
	 */
	public static boolean compareZero(BigDecimal bd) {
		int i = bd.compareTo(BigDecimal.ZERO);
		return   i == 1 || i == 0;
	}
	
	
	/**
	 * 减法运算
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static BigDecimal subtract(BigDecimal d1, BigDecimal d2) {
		return d1.subtract(d2);
	}

	/**
	 * 减法运算（四舍五入）
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static BigDecimal subtracts(BigDecimal d1, BigDecimal d2) {

		return d1.divide(d2, 2, RoundingMode.HALF_UP);
	}
	/**
	 * 除法运算
	 * 
	 * @param String d1
	 * @param String d2
	 * @return BigDecimal
	 */
	public static BigDecimal divide(String d1, String d2) {
		BigDecimal b4 = new BigDecimal(d1);
		BigDecimal b5 = new BigDecimal(d2);
		
		BigDecimal b3 = b4.divide(b5);

		return b3;
	}

	/**
	 * 加法运算
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static BigDecimal add(BigDecimal d1, BigDecimal d2) {
		return d1.add(d2);
	}

	/**
	 * 比较当前时间与传入时间的时间差是否小�?分钟
	 * @param nDate 传入时间
	 * @return <=5min :true   >5min :false
	 */
	public static boolean timeDifference(String nDate, long timeDiference) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long currentTime = 0;
        long systemTime = new Date().getTime();
        
        try {
        	if(isNoNull(nDate)){
        		currentTime = sdf.parse(nDate).getTime();
        	}else{
        		return false;
        	}
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timeDifferent = Math.abs(systemTime - currentTime);
        
        return timeDifferent <= timeDiference;
	}
	
	

	public static int monthcha(String createtime) {
		String oldyear = createtime.substring(0, 4);
		int yearcha = Integer.parseInt(oldyear);

		String monthold = createtime.substring(5, 7);
		int montholdint = Integer.parseInt(monthold);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date dNow = new Date(); // 当前时间
		String nDate = sdf.format(dNow);
		String newd = nDate.substring(0, 4);

		int newyear = Integer.parseInt(newd);

		String monthnew = nDate.substring(5, 7);

		int monthnewint = Integer.parseInt(monthnew);

		int monthcha = (newyear - yearcha) * 12 + (monthnewint - montholdint);
		return monthcha;
	}

	/**
	 * 取绝对�?
	 */
	public static Integer abs(Integer i) {

		return Math.abs(i);

	}

	/**
	 * 是否为数�?
	 * 
	 * @param value
	 * @return boolean
	 */
	public static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 变万�?加�?�?
	 */
	public static String jdh(BigDecimal value) {
		BigDecimal d2 = new BigDecimal("1000");
		BigDecimal b3 = value.divide(d2);
		String newm = new java.text.DecimalFormat("#.00").format(b3);
		StringBuffer newPrice = new StringBuffer();
		newPrice.append(newm);
		int i = newPrice.lastIndexOf(".");
		for (int j = i - 3; j > 0; j = j - 3) {
			newPrice.insert(j, ",");
		}
		return newPrice.toString();
	}
	/**
	 * 和今年的年差
	 * @param year
	 * @return
	 */
	public static int yearcha(String year){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date dNow = new Date(); // 当前时间
		String nDate = sdf.format(dNow);
	
		int cha=(Integer.parseInt(nDate)-Integer.parseInt(year));
		return cha;
	}
	/**
	 * 得到今年
	 * @param year
	 * @return
	 */
	public static String getyear(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date dNow = new Date(); // 当前时间
		String nDate = sdf.format(dNow);
	
	
		return nDate;
	}
	/**
	 * 判断字符串不为null并且不为�?
	 * @param str
	 * @return boolean 字符串不为null并且不为�?true
	 */
	public static boolean isNoNull(Object obj){
		return obj != null && !"".equals(obj);
	}
	/**
	 * 获取26个字�?10个数字的随机�?
	 * param int pwd_len 随机码长�?
	 * @return String 随机�?
	 */
	 public static String genRandomNum(int pwd_len){
		  //35是因为数组是�?�?��的，26个字�?10个数�?
		  final int  maxNum = 36;
		  int i;  //生成的随机数
		  int count = 0; //生成的密码的长度
		  char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
		    'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
		    'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
		  
		  StringBuffer pwd = new StringBuffer("");
		  Random r = new Random();
		  while(count < pwd_len){
		   //生成随机数，取绝对�?，防止生成负数，
		   
		   i = Math.abs(r.nextInt(maxNum));  //生成的数�?���?6-1
		   
		   if (i >= 0 && i < str.length) {
		    pwd.append(str[i]);
		    count ++;
		   }
		  }
		  
		  return pwd.toString();
		 }
	/**
	 * 获取gson,并转换json
	 */
	 public static String toJson(Object obj){
		 return new GsonBuilder().disableHtmlEscaping().create().toJson(obj);
	 }
	 
	 private static boolean isMatch(String regex, String orginal){
	        if (orginal == null || orginal.trim().equals("")) {  
	            return false;  
	        }  
	        Pattern pattern = Pattern.compile(regex);  
	        Matcher isNum = pattern.matcher(orginal);  
	        return isNum.matches();  
	    }  
	  
	    public static boolean isPositiveInteger(String orginal) {  
	        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);  
	    }  
	  
	    public static boolean isNegativeInteger(String orginal) {  
	        return isMatch("^-[1-9]\\d*", orginal);  
	    }  
	  
	    public static boolean isWholeNumber(String orginal) {  
	        return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);  
	    }  
	      
	    public static boolean isPositiveDecimal(String orginal){  
	        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);  
	    }  
	      
	    public static boolean isNegativeDecimal(String orginal){  
	        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);  
	    }  
	      
	    public static boolean isDecimal(String orginal){  
	        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);  
	    }  
	      
	    public static boolean isRealNumber(String orginal){  
	        return isWholeNumber(orginal) || isDecimal(orginal);  
	    }  
	
}